package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utilities.ArgumentParser;
import utilities.CommandFileParser;
import utilities.Help;
import entities.ParamsObject;
import enumeration.Operation;
import exception.InvalidInputException;

/**
 * 
 * @author ahosick this will replace a portion of an xml file with the contents
 *         of another xml file
 * 
 *         the main method needs to be passed five arguments, all strings 1) the
 *         name of the file to be altered 2) the name of the testName 3) the
 *         name of the piece to be inserted 4) the name of the node to be
 *         replaced
 */
public class Main {
	private static final Logger LOG = LoggerFactory.getLogger(Main.class);
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LOG.info("****Start main****\n");
		// variables
		List<String> argStrings = new ArrayList<String>();
		List<ParamsObject> paramObjects = new ArrayList<ParamsObject>();
		boolean fromFile = false;
		
		try {
			//TODO for testing
			//System.setIn(new FileInputStream("exampelCommandBatch.txt"));
			
			
			//Identify inputs
			if (0 < System.in.available()) {
				BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
				//parse text file
				//return list of strings in parsable format
				argStrings = CommandFileParser.parseFile(reader);
				fromFile = true;
				reader.close();
				
				if ( 0 < args.length) {
					System.out.println("Arguments in command line ignored, running XJM from command file");
				}
			}else if ( 0 < args.length) {
				//convert array into string and put in the list
				argStrings.add(ArgumentParser.stringMaker(args));
			}else{
				System.out.println("no arguments found");
			}
			
			//parse each argString into a paramsObject
			for (String arg : argStrings){
				paramObjects.add(ArgumentParser.parseArguments(arg, fromFile));
			}
			
			//Execute each operation
			for (ParamsObject po : paramObjects){
				runOperation(po);
			}
		
			
		} catch (IOException ioe) {
			LOG.error("***I/O exception***");
			System.out.println("Unable to open command file");
			
		}catch (InvalidInputException iie) {
			LOG.error("***Error parsing exception***");
			System.out.println("Failure to parse arguments: " + iie.getMessage());
			
		} catch (Exception e) {
			LOG.error("***User entered bad arguments***");
			System.out.println("Failure to run XJM: " + e.getMessage());
			
		}
		System.out.println();
		LOG.info("****End***");
	}
	
	
	public static void runOperation(ParamsObject po) throws Exception{
		String output;

		switch (po.getOperation()) {
		case HELP: 
			Help.help(Operation.HELP);
			break;

		case CHANGE_JMETER_NODE: 
			output = po.getOutputFile();
			
			if(null == output){
				po.setOutputFile(po.getFileName());
			}
			OperationsController.changeJmeterNode(po.getOutputFile(), po.getFileName(), po.getXmlPiece(), po.getTestName(), po.isFromFile());

			break;
		case HELP_CHANGE_JMETER_NODE:
			Help.help(Operation.CHANGE_JMETER_NODE);
			break;

		case SWAP_JMETER_NODE: 
			output = po.getOutputFile();
			
			if(null == output){
				po.setOutputFile(po.getFileName());
			}
			OperationsController.swapJmeterNode(po.getOutputFile(), po.getFileName(), po.getXmlPiece(), po.getXmlTestName(), po.getTestName(), po.isFromFile());

			break;
		case HELP_SWAP_JMETER_NODE:
			Help.help(Operation.SWAP_JMETER_NODE);
			break;
			
		case CHANGE_JMETER_ATTRIBUTE: 
			output = po.getOutputFile();
			
			if(null == output){
				po.setOutputFile(po.getFileName());
			}
			OperationsController.changeJmeterAttribute(po.getOutputFile(), po.getFileName(), po.getTestName(), po.getAttribute(), po.getUserInput(), po.isFromFile());//changes an attribute of a node with a user input for the attribute 
			break;
			
		case HELP_CHANGE_JMETER_ATTRIBUTE:
			Help.help(Operation.CHANGE_JMETER_ATTRIBUTE);
			break;
			
		case ANALYZE:
			output = po.getOutputFile();
			
			if(null == output ){
				po.setOutputFile(po.getFileName());
			}
			OperationsController.analyzeJmeterAssertion(po.getFileName(), po.getOutputFile(), po.isFromFile());//analyzes and finds assertions with multiple patterns
			break;
			
		case HELP_ANALYZE:
			Help.help(Operation.ANALYZE);
			break;
			
		default:
			LOG.error("You have not entered a correct combination of parameters.  Use -help for help");
			System.out.println("You have not entered a correct combination of parameters.  Use -help for help");
		}
	}
}
